package
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.game.core.GlobalInterface;
	import com.game.core.frame.FrameManager;
	import com.game.core.res.GameResource;
	import com.game.core.res.ProgressInfo;
	import com.game.core.res.Resource;
	import com.game.core.res.ResourceManager;
	import com.game.load.BatchLoad;
	import com.game.ui.PopUpManager;
	import com.game.ui.progress.SceProgressBarManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	import utils.ByteArrayUtil;
	
	[SWF(width=950 , height=760 , frameRate=40, backgroundColor=0x000000)]
	public class XGame extends Sprite
	{
		public function XGame()
		{
			addEventListener(Event.ADDED_TO_STAGE , onAdded);
		}
		
		private function onAdded(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			
			init();
		}
		
		private function init():void
		{
			stage.addEventListener(Event.ACTIVATE,activateHandler);
			stage.addEventListener(Event.DEACTIVATE,deactivateHandler);
			stage.addEventListener(Event.ENTER_FRAME,onFrame);
			
			GlobalInterface.init(this);
			FrameManager.getInstance().run();
			PopUpManager.init(GlobalInterface.popUpContainer);
			ByteArrayUtil.keys = "ouyangzhongming";
			var customTypesExtensions : Object = {};
			customTypesExtensions[BulkLoader.TYPE_BINARY] = ['config'];
			BulkLoader._customTypesExtensions = customTypesExtensions;
			ResourceManager.getInstance().addCompress2cryptExtensions('config');
			ResourceManager.getInstance().init(StaticURL.filesystemUrl,onSystemFileComplete);
		}
		
		private function onSystemFileComplete(bool:Boolean):void
		{
			trace("下载完成"+int(bool));
			Resource.getResource("xbg.swf").onComplete(xbgComplete).onIOError(xbgIOErro).onProgress(xbgProgress).load();
		}
		
		private function xbgProgress(e:ProgressEvent):void
		{	
			trace("e.bytesLoaded ==" +e.bytesLoaded);
			trace("e.bytesLoaded1 ==" +e.bytesTotal);
		}
		
		private function xbgComplete(res:GameResource):void
		{
			var cla:Class = res.getClass("mainBackground");
			var image : BitmapData = new cla(0,0);
			var bitmap:Bitmap = new Bitmap(image);
			GlobalInterface.gameContainer.addChild(bitmap);
			res.unload();
			
			Resource.getResource("1.png").onComplete(pngComplete).onIOError(pngIOErro).load();
		}
		
		private var skin:MovieClip;
		private var maskUi : MovieClip;
		private function pngComplete(res:GameResource):void
		{
			var bit:Bitmap = new Bitmap(res.getImage());
			bit.x = 100;
			bit.y = 100;
			GlobalInterface.uiContainer.addChild(bit);
			
//			skin = new UiPorgressBar() as MovieClip;
//			GlobalInterface.uiContainer.addChild(skin);
//			maskUi = skin["maskUi"];
//			maskUi.scaleX = 0;
			
			SceProgressBarManager.getInstance().resTotal = 7670416;
			SceProgressBarManager.getInstance().reset();
			SceProgressBarManager.getInstance().show();
			
			var batchLoad : BatchLoad = new BatchLoad();
			batchLoad.initData();
		}
		
		private function onFrame(e:Event):void
		{
			if(skin != null)
			{
				maskUi.scaleX += 0.001;
				var count:int = maskUi.scaleX*100;
				skin["txtProgress"].text = "我正在努力加载："+count+"%";
				if(maskUi.scaleX >= 1)
					maskUi.scaleX = 0;
			}
		}
		
		private function pngIOErro(res:GameResource):void
		{
		}
		
		private function xbgIOErro(res:GameResource):void
		{
		}
		
		private function activateHandler(e:Event):void
		{
		}
		
		private function deactivateHandler(e:Event):void
		{
		}
		
		/**
		 * 开始资源批加载
		 */
		private function startBatchLoad() : void
		{
		}
		
	}
}